package com.theo.mapper;

import com.theo.entity.TopicInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.sf.jsqlparser.statement.select.Top;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Repository
public interface TopicInfoMapper extends BaseMapper<TopicInfo> {

    int saveTopicInfo(TopicInfo topicInfo);

    int deleteTopicInfo(Integer id);

    int updateTopicInfo(Integer id);

    List<TopicInfo> queryAllTopicInfo();

    @Select("SELECT e.`topic_id`,e.`user_id`,e.`login_name`,e.`topic_type_id`,t.`type_name` `topic_type_name`,e.`topic_title`,e.`topic_content`,e.`topic_views`,e.`create_time`\n" +
            "FROM `type_info` t INNER JOIN(\n" +
            "SELECT a.`topic_id`,a.`user_id`,d.`login_name`,`topic_type_id`,`topic_title`,`topic_content`,`create_time`,`topic_views`\n" +
            "FROM `topic_info` a INNER JOIN (\n" +
            "\tSELECT DISTINCT b.`user_id`,c.`login_name`\n" +
            "\tFROM `topic_info` b INNER JOIN`user_info` c\n" +
            "\tON b.`user_id`=c.`user_id`\n" +
            ") d ON a.`user_id`=d.`user_id`\n" +
            ") e ON t.`type_id`=e.`topic_type_id`")
    List<TopicInfo> queryAllTopicInfoByPage();

    @Select("SELECT e.`topic_id`,e.`user_id`,e.`login_name`,e.`topic_type_id`,t.`type_name` `topic_type_name`,e.`topic_title`,e.`topic_content`,e.`topic_views`,e.`create_time`\n" +
            "FROM `type_info` t INNER JOIN(\n" +
            "SELECT a.`topic_id`,a.`user_id`,d.`login_name`,`topic_type_id`,`topic_title`,`topic_content`,`create_time`,`topic_views`\n" +
            "FROM `topic_info` a INNER JOIN (\n" +
            "\tSELECT DISTINCT b.`user_id`,c.`login_name`\n" +
            "\tFROM `topic_info` b INNER JOIN`user_info` c\n" +
            "\tON b.`user_id`=c.`user_id`\n" +
            ") d ON a.`user_id`=d.`user_id`\n" +
            ") e ON t.`type_id`=e.`topic_type_id`\n" +
            "WHERE topic_type_id = #{id}")
    List<TopicInfo> queryAllByTopicTypeId(String id);

    TopicInfo queryTopicInfoById(Integer id);
}
